package com.dove.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dove.entity.Menu;
import com.dove.mapper.MenuMapper;
import com.dove.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dove
 * @since 2022-03-26
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {


    @Override
    public List<Menu> findMenus(String name) {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        if (StrUtil.isNotBlank(name)) {
            qw.like("name", name);
        }
        List<Menu> list = list(qw);
        //找出一级菜单
        List<Menu> parentNodes = list.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        for (Menu parentNode : parentNodes) {
            //遍历一级菜单找出对应二级子菜单并添加
            List<Menu> children = list.stream().filter(menu -> parentNode.getId().equals(menu.getPid())).collect(Collectors.toList());
            parentNode.setChildren(children);
        }
        return parentNodes;
        //return list;
    }
}
